package com.llkj.contractmanagement.contractmanagement.mine;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.llkj.contractmanagement.main.BaseActivity;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.contractmanagement.adapter.CollectionDeleteAdapter;
import com.llkj.contractmanagement.customview.TitleBar;
import com.llkj.contractmanagement.http.HttpMethod;
import com.llkj.contractmanagement.http.UrlConfig;
import com.llkj.contractmanagement.model.CollectionBean;
import com.llkj.contractmanagement.model.UserInfoBean;
import com.llkj.contractmanagement.utils.GsonUtil;
import com.llkj.contractmanagement.utils.LogUtil;
import com.llkj.contractmanagement.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;

/**
 * author: dong.yj
 * created on: 2016/8/19 14:49
 * description:我的收藏-取消关注页面
 *
 */
public class EditMyCollectionActivity extends BaseActivity implements AdapterView.OnItemClickListener, View.OnClickListener {
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_editmycollection;
    }

    @Bind(R.id.title_collectiondelete)
    TitleBar titleBar;
    @Bind(R.id.lv_collection_delete)
    ListView lv_conllection;
    @Bind(R.id.btn_delete)
    Button btn_delete;

    private List<CollectionBean.YrlistBean> collectionBeanList;
    private CollectionDeleteAdapter adapter;
    private UserInfoBean userInfoBean;
    private StringBuffer stringBuffer;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        init();
        setListener();
    }

    private void init() {
        userInfoBean = UserInfoBean.getInstance();
        stringBuffer = new StringBuffer();
        collectionBeanList = new ArrayList<>();
        adapter = new CollectionDeleteAdapter(this, collectionBeanList);
        lv_conllection.setAdapter(adapter);
        //请求收藏列表接口
        yrCollectionList();
    }

    private void yrCollectionList() {
        HashMap<String, String> map = new HashMap<>();
        map.put("user_id",userInfoBean.getId());
        map.put("token", userInfoBean.getToken());
        HttpMethod.yrCollection(this, this, map, UrlConfig.HTTP_YRCOLLECTION);
    }

    private void setListener() {
        titleBar.setTopBarClickListener(this);
        lv_conllection.setOnItemClickListener(this);
        btn_delete.setOnClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CollectionDeleteAdapter.ViewHolder holder = (CollectionDeleteAdapter.ViewHolder) view.getTag();
        holder.checkBox.toggle();
        collectionBeanList.get(position).setFalg(holder.checkBox.isChecked());
        stringBuffer.append(collectionBeanList.get(position).getId()).append(",");
        LogUtil.e("sc_id" + stringBuffer.toString());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_delete:
                delAllCollection();
                break;
        }
    }

    private void delAllCollection() {
        if (TextUtils.isEmpty(stringBuffer.toString())) {
            ToastUtil.makeShortText(this, "请选择您要取消关注的艺人");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
//        user_id	1	string	用户的id
//        token	1	string	Token
//        sc_id	1	string	收藏ID
        map.put("user_id", userInfoBean.getId());
        map.put("token", userInfoBean.getToken());
        map.put("sc_id", stringBuffer.toString().substring(0, stringBuffer.toString().length() - 1));
        HttpMethod.delCollectionAll(this, this, map, UrlConfig.HTTP_DELCOLLECTIONALL);
    }

    @Override
    public void onSuccessHttp(String responseInfo, int resultCode) {
        super.onSuccessHttp(responseInfo, resultCode);
        switch (resultCode) {
            case UrlConfig.HTTP_YRCOLLECTION:
                CollectionBean resultBean = GsonUtil.GsonToBean(responseInfo, CollectionBean.class);
                if (resultBean != null) {
                    List<CollectionBean.YrlistBean> addList = resultBean.getYrlist();
                    if (addList != null && addList.size() > 0) {
                        collectionBeanList.addAll(addList);
                        adapter.notifyDataSetChanged();
                    }
                }
                break;
            case UrlConfig.HTTP_DELCOLLECTIONALL:
                ToastUtil.makeShortText(this, "取消关注成功");
                this.finish();
                break;
        }
    }
}
